﻿using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Provenance.Infra
{
    public class BaseRepository<T>: IBaseRepository<T> where T : class, new()
    {
        private readonly ProvenanceDbContext _context;
        private readonly ILogger<BaseRepository<T>> _logger;
       public BaseRepository(ProvenanceDbContext context, ILogger<BaseRepository<T>> logger)
        {
            _context = context;
            _logger = logger;
        }
        public async Task<IList<T>> GetAllAsync()
        {
            try
            {
                return await _context.Set<T>().ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败"+ex.Message);
                throw;
            }
        }
        public async Task<IList<T>> GetAllAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                return await _context.Set<T>().Where(expression).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<T?> GetAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                return await _context.Set<T>().FirstOrDefaultAsync(expression);
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<int> InsertEntity(T entity)
        {
            try
            {
                await _context.Set<T>().AddAsync(entity);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<int> UpdateEntity(T entity)
        {
            try
            {
                _context.Set<T>().Update(entity);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<int> DeleteEntity(T entity)
        {
            try
            {
                _context.Set<T>().Remove(entity);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<int> InsertRangeEntity(IList<T> entities)
        {
            try
            {
                await _context.Set<T>().AddRangeAsync(entities);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
        public async Task<int> UpdateRangeEntity(IList<T> entities)
        {
            try
            {
                _context.Set<T>().UpdateRange(entities);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
      
        public async Task<int> DeleteRangeEntity(IList<T> entities)
        {
            try
            {
                _context.Set<T>().RemoveRange(entities);
                return await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }


    }
}
